package cn.itcast.mq.listener;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.HashMap;

@Component
public class SpringRabbitListener {

//    @RabbitListener(queues = "simple.queue")
//    public void ListenQueue(String msg) throws InterruptedException {
//        System.err.println("消费者接收到 simple.queue 的消息:" + msg + LocalDate.now());
//        Thread.sleep(100);
//    }
//
//    @RabbitListener(queues = "simple.queue")
//    public void ListenQueue2(String msg) throws InterruptedException {
//        System.out.println("消费者接收到 simple.queue 的消息:" + msg + LocalDate.now());
//        Thread.sleep(25);
//    }

    @RabbitListener(queues = "fanout.queue1")
    public void ListenFanoutQueue1(String msg) {
        System.out.println("消费者接收到 fanout.queue1 的消息:" + msg + LocalDate.now());
    }

    @RabbitListener(queues = "fanout.queue2")
    public void ListenFanoutQueue2(String msg) {
        System.out.println("消费者接收到 fanout.queue2 的消息:" + msg + LocalDate.now());
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue1"), //绑定队列
            exchange = @Exchange(name = "itcast.direct", type = ExchangeTypes.DIRECT), //绑定交换机，type 可以不写，默认为 Direct
            key = {"java", "C++"} // Bindingkey
    ))
    public void listenDirectQueue1(String msg) {
        System.out.println("消费者 1 收到 Direct 消息：" + msg);
    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue2"), //绑定队列
            exchange = @Exchange(name = "itcast.direct", type = ExchangeTypes.DIRECT), //绑定交换机，type 可以不写，默认为 Direct
            key = {"java", "GO"} // Bindingkey
    ))
    public void listenDirectQueue2(String msg) {
        System.out.println("消费者 2 收到 Direct 消息：" + msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("topic.queue1"),
            exchange = @Exchange(name = "itcast.topic", type = ExchangeTypes.TOPIC),
            key = "china.#"
    ))
    public void listenTopicQueue1(String msg) {
        System.out.println("消费者 1 收到 topic 消息：" + msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("topic.queue2"),
            exchange = @Exchange(name = "itcast.topic", type = ExchangeTypes.TOPIC),
            key = "#.good"
    ))
    public void listenTopicQueue2(String msg) {
        System.out.println("消费者 2 收到 topic 消息：" + msg);
    }

//    @RabbitListener(queues = "object.queue")
//    public void listenObjectQueue(HashMap<String, Object> map) {
//        System.out.println("Object 队列收到数据:" + map);
//    }



}
